// All the basic functions

function doNothing () {}


// DOM Utils
function $(id) {
	return document.getElementById(id);
}

function Widget (tag, id, widgetClass) {	
	var widget = document.createElement(tag);
	if (id) widget.id = id;
	if (widgetClass) widget.className = widgetClass;
	return widget;
}

function Link (linkTo, inner, target) {
	var el = document.createElement('a');
	el.href = linkTo;
	el.innerHTML = inner;
	if (target) el.target = target
	return el;
}

function getHTML (el) {
	ret = document.createElement('div');
	ret.appendChild(el);
	return ret.innerHTML;
}

function Text (text) {
	return document.createTextNode(text);
}



// AJAX Basics
function getTransport () {
	if (window.ActiveXObject) {
		try {
			var ajax = new ActiveXOject('Microsoft.XMLHTTP');
		} catch (e) {
			var ajax = new ActiveXObject('Msxml2.XMLHTTP');
		}
		return ajax;
	} else {
		return (new XMLHttpRequest());
	}
}

function requestFile(url, callback, extarg) {
	var transport = getTransport();
	transport.onreadystatechange = function () {
		callback(transport, extarg);
	}
	transport.open('get', url, true);
	transport.send(null);
	return true;
}

function callJSON (url, callback, extarg) {
	var transport = getTransport();
	transport.onreadystatechange = function () {
		if (transport.readyState != 4 || transport.status != 200)
			return false;

		return callback(eval(transport.responseText),
				extarg);
	};
	transport.open('get', url, true);
	transport.send(null);
	
	return true;
}

function getResource () {
	var idx = document.location.href.indexOf('#');
	if (idx == -1) {
		return '';
	} else {
		return document.location.href.substring(idx + 1);
	}	
}

function setResource (resource) {
	var idx = document.location.href.indexOf('#');
	var next = '';
	
	if (idx == -1) {
		next = document.location.href + '#' + resource;
	} else {
		next = document.location.ref.substring(0, idx + 1);
		next += resource;
	}

	document.location.replace(next);
	return true
}

// Utils

String.prototype.escapeHTML = function () {
	var div = document.createElement('div');
	var text = document.createTextNode(this);
	div.appendChild(text);
	return div.innerHTML;
};


String.prototype.strip = function () {
	return this.replace(/^\s+/, '').replace(/\s+$/, '');
};


// Gloabl State ========================

gData = {};
gData.currResource = '';
gData.currPage = 0;
gData.openThread = -1;

